package volleyball.model;

import java.util.HashSet;
import java.util.Iterator;


/**
 * The top layer of the storage model. Stores info about a game as well as the
 * the players in it and their stats.
 * @author Joshua Kulhavy-Sutherland
 * 
 * ADT: [Player, Player, Player, Player] - stats for each player
 * date - when the game was played
 * gameType - eventually will be an enum
 * round - eventually will be an enum
 * 
 */
public class Game {
	private HashSet<Player> players = new HashSet<Player>();
	private String description;
	private String date;
	private String gameType;
	private String round;
	
	/**
	 * Initializes a Game with 4 players named player1, player2, player3, and player4
	 * respectively. 
	 */
	public Game(){
		players.add(new Player("player1"));
		players.add(new Player("player2"));
		players.add(new Player("player3"));
		players.add(new Player("player4"));
	}
	
	/**
	 * @return the description or "No Description Available." if description is null.
	 */
	public String getDescription() {
		if (description == null){
			return "No Description Available.";
		}
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the date or "No Date Available." if date is null.
	 */
	public String getDate() {
		if (date == null){
			return "No Date Available.";
		}
		return date;
	}

	/**
	 * @param date the date to set
	 */
	public void setDate(String date) {
		this.date = date;
	}

	/**
	 * @return the gameType or "No Game Type Available." if gameType is null.
	 */
	public String getGameType() {
		if (gameType == null){
			return ("No Game Type Available.");
		}
		return gameType;
	}

	/**
	 * @param gameType the gameType to set
	 */
	public void setGameType(String gameType) {
		this.gameType = gameType;
	}

	/**
	 * @return the round or "No Round Available." if round is null.
	 */
	public String getRound() {
		if (round == null){
			return "No Round Available.";
		}
		return round;
	}

	/**
	 * @param round the round to set
	 */
	public void setRound(String round) {
		this.round = round;
	}
	
	/**
	 * @return String a String that contains all of the players name's and statistics, as well as
	 * 		   the Game's description, gameType, round, and date.
	 */
	public String toString(){
		String playerPrintOuts = "";
		for(Player p : players){
			playerPrintOuts += p.toString()+"\n";
		}
		return playerPrintOuts + "Description: "+description+"\n Game Type: "+gameType+" Round: "+round+"" +
				"Date: "+date+"\n";
	}
	
	/**
	 * @param name The name of the player to fetch from this Game
	 * @return The Player with name
	 */
	public Player getPlayer(String name){
		for (Player p : players){
			if(p.getName().equals(name)){
				return p;
			}
		}
		return null;
	}
	
	/**
	 * Returns the player of number playerNumber in the set. Works like an array where 0 is the first 
	 * player, 1 is the second player, 2 is the third and so on and so forth.
	 * @param playerNumber The number of the player to be retrieved
	 * @return The player of number playerNumber in the Set
	 * @throws IndexOutOfBoundsException If playerNumber is bigger then the set
	 */
	public Player getPlayer(int playerNumber) throws IndexOutOfBoundsException{
		Iterator<Player> iterator = players.iterator();
		Player player = iterator.next();
		for(int x = 0; x<playerNumber; x++){
			if (iterator.hasNext()){
				player = iterator.next();
			} else {
				throw new IndexOutOfBoundsException();
			}
		}
		return player;
	}
	
	/**
	 * @param name Name of the Player to check for
	 * @return true if Game contains this player, false otherwise
	 */
	public boolean containsPlayer(String name) {
		if (players.contains(new Player(name)))
			return true;
		return false;
	}
}
